package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dao.StuDao;
import com.example.domain.Stu;
import com.example.service.IStuService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StuServiceImpl extends ServiceImpl<StuDao, Stu> implements IStuService {

    @Autowired
    private StuDao stuDao;

    @Override
    public boolean modify(Stu stu) {
        return stuDao.updateById(stu) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return stuDao.deleteById(id) > 0;
    }

    @Override
    public IPage<Stu> getPage(int currentPage, int pageSize) {
        IPage page = new Page(currentPage,pageSize);
        stuDao.selectPage(page,null);
        return page;
    }

    @Override
    public IPage<Stu> getPage(int currentPage, int pageSize, Stu stu) {
        LambdaQueryWrapper<Stu> lqw = new LambdaQueryWrapper<Stu>();
        lqw.like(Strings.isNotEmpty(stu.getName()),Stu::getName,stu.getName());
        lqw.like(Strings.isNotEmpty(stu.getPassword()),Stu::getPassword,stu.getPassword());
        lqw.like(stu.getGender() != null,Stu::getGender,stu.getGender());


        IPage page = new Page(currentPage,pageSize);
        stuDao.selectPage(page,lqw);
        return page;
    }
}
